package com.leetcode.LC;

public class LC509 {
    public int fib(int n) {

        if (n == 0) {
            return 0;
        }
        if (n == 1) {
            return 1;
        }
        int[] ints = new int[n + 1];
        ints[0] = 0;
        ints[1] = 1;
        for (int i = 2; i < n + 1; i++) {
            ints[i] = ints[i - 1] + ints[i - 2];
        }
        return ints[n];
    }

    public int fib2(int n) {

        if (n == 0) {
            return 0;
        }
        if (n == 1) {
            return 1;
        }
        int a = 0;
        int b = 1;
        int c = 0;
        for (int i = 2; i < n + 1; i++) {
            c = b + a;
            a=b;
            b=c;
        }
        return c;
    }

    public static void main(String[] args) {
        System.out.println(new LC509().fib2(4));
    }
}
